import Vue from 'vue'
import VueI18n from 'vue-i18n'
import zh from './zh'
import en from './en'
import enLocale from 'element-ui/lib/locale/lang/en'
import zhLocale from 'element-ui/lib/locale/lang/zh-CN'
import ElementLocale from 'element-ui/lib/locale'

Vue.use(VueI18n);

let lang = navigator.language?navigator.language:'zh-CN';
console.log(lang)
const messages = {
  'en-US': {
    ...enLocale,
    ...en 
  },
  'zh-CN': {
    ...zhLocale,
    ...zh
  }
}

lang = window.localStorage.getItem('locale') || lang;
window.localStorage.setItem('locale',lang);

Object.keys(messages).forEach(locale => {
  document.body.classList.remove('lang-' + locale);
})

document.body.classList.add('lang-' + lang);
document.body.setAttribute('lang', lang);
Vue.config.lang = lang;


const i18n = new VueI18n({
  locale: lang,
  messages
})

ElementLocale.i18n((key, value) => i18n.t(key, value));

if (module.hot) {
  module.hot.accept(['./en', './zh'], function () {
    i18n.setLocaleMessage('en', require('./en').default)
    i18n.setLocaleMessage('zh', require('./zh').default)
    // Or the following hot updates via $i18n property
    // app.$i18n.setLocaleMessage('en', require('./en').default)
    // app.$i18n.setLocaleMessage('ja', require('./ja').default)
  })
}

export default i18n